<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="cancel">
    <el-form label-width="120px" ref="deptForm" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input placeholder="2-10个字符" v-model="formData.name" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input placeholder="2-10个字符" v-model="formData.code" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select placeholder="请选择负责人" v-model="formData.managerId" style="width: 80%" size="mini">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input placeholder="1-100个字符" v-model="formData.introduce" type="textarea" size="mini" :rows="4"
          style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="confirmBtn">确定</el-button>
            <el-button size="mini" @click="cancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getManagerList, getDepartment, addDepartment, departmentDetail, updateDepartment } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }, currentNodeId: {
      type: Number,
      default: null
    },
  },
  created() {
    this.getManagerList()
    this.getDepartment()
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  data() {
    return {
      departmentList: [],
      managerList: [],
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      },
      rules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          {
            trigger: 'blur', validator: (rule, value, callback) => {
              if (this.departmentList.length == 0) {
                this.getDepartment()
              }
              if (this.formData.id) {
                this.departmentList = this.departmentList.filter(item => item.code != value)
              }
              if (this.departmentList.some(item => item.code === value)) {
                callback(new Error('部门编码已经存在'))
              } else {
                callback()
              }
            }
          }
        ], // 部门编码
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
          }], // 部门介绍
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          }, {
            trigger: 'blur', validator: (rule, value, callback) => {
              if (this.departmentList.length == 0) {
                this.getDepartment()
              }
              if (this.formData.id) {
                this.departmentList = this.departmentList.filter(item => item.name != value)
              }
              if (this.departmentList.some(item => item.name === value)) {
                callback(new Error('部门名称已经存在'))
              } else {
                callback()
              }
            }
          }] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      }
    }
  },

  methods: {
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    async getDepartment() {
      this.departmentList = await getDepartment()
    },
    cancel() {
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async departmentDetail() {
      this.formData = await departmentDetail(this.currentNodeId)
    },
    //getDepartment
    confirmBtn() {
      this.$refs.deptForm.validate(async isok => {
        if (isok) {
          if (this.formData.id) {
            await updateDepartment(this.formData)
            this.$message.success(`更新部门成功`)
          } else {
            await addDepartment({ ...this.formData, pid: this.currentNodeId })
            this.$message.success(`新增部门成功`)
          }

          this.$emit('updateDepartment')
          // 提示消息

          this.cancel()
        }
      })


    },
  }
}
</script>